﻿using System;

class Action
{
    private static int actions = 2;

    public static void UnitAction(Player p, Unit[] army1, Unit[] army2, string[,] board, int act)
    {
        string input = String.Empty;

        Console.WriteLine("Your turn" + p.PName);
        Console.WriteLine("Enter <unit> Command(move/attack) coordinateX coordinateY, or \"skip\" to skip your turn.\n Example archer move 2 2");
        input = Console.ReadLine();
        if (input != "skip")
        {
            string[] choice = input.Split(' ');

            if (choice.Length != 4)
            {
                Console.WriteLine("Wrong command input. Try Again.");
                UnitAction(p, army1, army2, board, actions);
            }
            else
            {


                try
                {
                    int.Parse(choice[2]);
                    int.Parse(choice[3]);
                }
                catch (FormatException ex)
                {
                    Console.WriteLine(ex.Message);
                    UnitAction(p, army1, army2, board, actions);
                    return;

                }
                if (int.Parse(choice[2]) < 1 || int.Parse(choice[2]) > 8 || int.Parse(choice[3]) < 1 || int.Parse(choice[3]) > 5)
                {
                    Console.WriteLine("Imposible coordinates");
                    UnitAction(p, army1, army2, board, actions);
                }
                switch (choice[0].ToLower())
                {
                    case "warrior":
                        {
                            if (army1[0].isDead())
                            {
                                Console.WriteLine("Unit doesn't exist.");
                                UnitAction(p, army1, army2, board, actions);
                                break;
                            }
                            switch (choice[1].ToLower())
                            {
                                case "move":
                                    {
                                        if (army1[0].Move(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }

                                case "attack":
                                    {
                                        if (army1[0].Attack(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            for (int i = 0; i < army2.Length; i++)
                                                if (army2[i].X == int.Parse(choice[2]) * 2 - 1 && army2[i].Y == int.Parse(choice[3]) * 2 - 1)
                                                {
                                                    army2[i].takeDMG(army1[0].doDMG());
                                                }

                                            break;
                                        }
                                        else { UnitAction(p, army1, army2, board, actions); break; }
                                    }
                                default: Console.WriteLine("wrong comamnd"); UnitAction(p, army1, army2, board, actions); break;
                            }
                            break;
                        }
                    case "knight":
                        {
                            if (army1[1].isDead())
                            {
                                Console.WriteLine("Unit doesn't exist.");
                                UnitAction(p, army1, army2, board, actions);
                                break;
                            }
                            switch (choice[1].ToLower())
                            {
                                case "move":
                                    {
                                        if (army1[1].Move(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }

                                case "attack":
                                    {
                                        if (army1[1].Attack(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            for (int i = 0; i < army2.Length; i++)
                                                if (army2[i].X == int.Parse(choice[2]) * 2 - 1 && army2[i].Y == int.Parse(choice[3]) * 2 - 1)
                                                {
                                                    army2[i].takeDMG(army1[1].doDMG());
                                                }

                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }
                                default: Console.WriteLine("wrong comamnd"); UnitAction(p, army1, army2, board, actions); break;
                            }
                            break;
                        }
                    case "archer":
                        {
                            if (army1[2].isDead())
                            {
                                Console.WriteLine("Unit doesn't exist.");
                                UnitAction(p, army1, army2, board, actions);
                                break;
                            }
                            switch (choice[1].ToLower())
                            {
                                case "move":
                                    {
                                        if (army1[2].Move(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }

                                case "attack":
                                    {
                                        if (army1[2].Attack(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            for (int i = 0; i < army2.Length; i++)
                                                if (army2[i].X == int.Parse(choice[2]) * 2 - 1 && army2[i].Y == int.Parse(choice[3]) * 2 - 1)
                                                {
                                                    army2[i].takeDMG(army1[2].doDMG());
                                                }

                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }
                                default: Console.WriteLine("wrong comamnd"); UnitAction(p, army1, army2, board, actions); break;
                            }
                            break;
                        }
                    case "barbarian":
                        {
                            if (army1[3].isDead())
                            {
                                Console.WriteLine("Unit doesn't exist.");
                                UnitAction(p, army1, army2, board, actions);
                                break;
                            }
                            switch (choice[1].ToLower())
                            {
                                case "move":
                                    {
                                        if (army1[3].Move(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }

                                case "attack":
                                    {
                                        if (army1[3].Attack(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            for (int i = 0; i < army2.Length; i++)
                                                if (army2[i].X == int.Parse(choice[2]) * 2 - 1 && army2[i].Y == int.Parse(choice[3]) * 2 - 1)
                                                {
                                                    army2[i].takeDMG(army1[3].doDMG());
                                                }

                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }
                                default: Console.WriteLine("wrong comamnd"); UnitAction(p, army1, army2, board, actions); break;
                            }
                            break;
                        }
                    case "pikeman":
                        {
                            if (army1[4].isDead())
                            {
                                Console.WriteLine("Unit doesn't exist.");
                                UnitAction(p, army1, army2, board, actions);
                                break;
                            }
                            switch (choice[1].ToLower())
                            {
                                case "move":
                                    {
                                        if (army1[4].Move(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }

                                case "attack":
                                    {
                                        if (army1[4].Attack(int.Parse(choice[2]), int.Parse(choice[3]), board))
                                        {
                                            for (int i = 0; i < army2.Length; i++)
                                                if (army2[i].X == int.Parse(choice[2]) * 2 - 1 && army2[i].Y == int.Parse(choice[3]) * 2 - 1)
                                                {
                                                    army2[i].takeDMG(army1[4].doDMG());
                                                }

                                            break;
                                        }
                                        else
                                        {
                                            UnitAction(p, army1, army2, board, actions);
                                            break;
                                        }
                                    }
                                default: Console.WriteLine("wrong comamnd"); UnitAction(p, army1, army2, board, actions); break;
                            }
                            break;
                        }
                    default: Console.WriteLine("wrong unit"); UnitAction(p, army1, army2, board, actions); break;
                }
            }
        }
    }
}

